package server.business.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import server.business.pojo.Patient;
import server.business.service.PatientService;
import server.business.mapper.PatientMapper;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author Galaxy
 * @description 针对表【patient】的数据库操作Service实现
 * @createDate 2022-11-08 12:46:24
 */
@Service("patientService")
public class PatientServiceImpl extends ServiceImpl<PatientMapper, Patient>
        implements PatientService {
    @Autowired
    private PatientMapper patientMapper;

    /**
     * fzx
     * 
     * @param medicalRecordNumber
     * @description 返回个人信息
     * @return 个人信息列表
     */
    public List<Patient> getPatientById(String medicalRecordNumber) {
        LambdaQueryWrapper<Patient> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Patient::getMedicalRecordNumber, medicalRecordNumber);
        List<Patient> list = this.list(wrapper);
        return list;
    }

    // *************************************myq*************************start
    @Override
    public int patientAdd(Patient patient) {
        int patientCreate = patientMapper.insert(patient);
        return patientCreate;
    }

    @Override
    public Page<Patient> getPatientPage() {
        Page<Patient> page = new Page<>(1, 5);
        patientMapper.selectPage(page, null);
        page.getRecords().forEach(System.out::println);
        long pageSize1 = page.getSize();
        System.out.println(pageSize1);
        System.out.println(page.getTotal());
        return page;

    }

    @Override
    public Long selectPatientId(String medicalRecordNumber) {
        QueryWrapper<Patient> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("medical_record_number", medicalRecordNumber);
        Patient patient = patientMapper.selectOne(queryWrapper);
        Long patientId = patient.getId();
        return patientId;
    }

    /**
     * 分页查询+按条件查询
     *
     * @param currentPage
     * @param pageSize
     * @param patient
     * @return
     */
    @Override
    @SuppressWarnings("")
    public IPage<Patient> getPage(int currentPage, int pageSize, Patient patient) {
        LambdaQueryWrapper<Patient> queryWrapper = new LambdaQueryWrapper<Patient>();
        queryWrapper
                .eq(Strings.isNotBlank(patient.getName()), Patient::getName, patient.getName())
                .eq(Strings.isNotBlank(patient.getMedicalRecordNumber()), Patient::getMedicalRecordNumber,
                        patient.getMedicalRecordNumber())
                .eq(Strings.isNotBlank(patient.getIdcard()), Patient::getIdcard, patient.getIdcard());
        IPage<Patient> page = new Page<>(currentPage, pageSize);
        patientMapper.selectPage(page, queryWrapper);
        return page;

    }

    /**
     * 根据病历号自动回填身份证号
     * @param medicalRecordNumber
     * @return
     */
    @Override
    public String getIdcard(String medicalRecordNumber) {
        LambdaQueryWrapper<Patient> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Strings.isNotBlank(medicalRecordNumber), Patient::getMedicalRecordNumber,medicalRecordNumber);
        System.out.println(medicalRecordNumber);
        Patient result = patientMapper.selectOne(queryWrapper);
        if (result == null) {
            return null;
        }
        return result.getIdcard();
    }

    // *************************************myq*************************end

    // *************************************lrz*************************start
    @Override
    public Long getIdByIdcard(String patientIdcard) {
        LambdaQueryWrapper<Patient> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Strings.isNotBlank(patientIdcard), Patient::getIdcard, patientIdcard);
        Patient result = patientMapper.selectOne(queryWrapper);
        if (result == null) {
            return null;
        }
        return result.getId();
    }

    /**
     * 根据患者Id找到身份证号
     */
    @Override
    public String getIdcardById(Long Id) {
        LambdaQueryWrapper<Patient> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Patient::getId, Id);
        Patient result = patientMapper.selectOne(queryWrapper);
        if (result == null) {
            return null;
        }
        return result.getIdcard();
    }

    // *************************************lrz*************************end

}
